package com.glodon.paas.account.dao.mysql;

import java.util.Date;
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired;
 
import com.glodon.paas.account.dao.CommunicateBookDao;
import com.glodon.paas.account.dao.domain.CommunicateEntity;
import com.glodon.paas.account.dao.mapper.CommunicateEntityMapper;
import com.glodon.paas.consts.DatabaseConst;
 
public class CommunicateBookDaoMysqlImpl implements CommunicateBookDao {
	@Autowired
	private CommunicateEntityMapper communicateEntityMapper;
	
	@Override
	public String insert(CommunicateEntity entity) {
		if(entity.getId() == null){
			return null;
		}
		
		if(entity.getCreateTime() == null){
			entity.setCreateTime(new Date());
		} 
		
		entity.setUpdateTime(entity.getCreateTime());
		communicateEntityMapper.insertSelective(entity);
		return entity.getId();
	}

	@Override
	public CommunicateEntity get(String id) {
		return communicateEntityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String id) {
		return communicateEntityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteById(String id) {
		return communicateEntityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByContactId(String userId, String contactId) {		
		return communicateEntityMapper.deleteByContactId(userId, contactId);
	}

	@Override
	public int deleteAllByUserId(String userId) {
		if(userId == null){
			return 0;
		}		
		return communicateEntityMapper.deleteAllByUserId(userId);
	}

	@Override
	public int updateById(CommunicateEntity communicate) {
		return communicateEntityMapper.updateByPrimaryKeySelective(communicate);
	}

//	@Override
//	public int updateRemark(String id, String remarkName, String remarkText) {
//		CommunicateEntity communicateEntitynew = new CommunicateEntity();
//		communicateEntitynew.setId(id);
//		communicateEntitynew.setRemarkName(remarkName);
//		communicateEntitynew.setRemarkText(remarkText);
//		communicateEntitynew
//		return 0;
//	}

	@Override
	public List<CommunicateEntity> selectByUserId(String userId,
			int orderType, int startIndex, int count) {
		String order = null;
		if(orderType == DatabaseConst.ORDER_BY_ASC_INT){
			order = "asc";
		}else if(orderType == DatabaseConst.ORDER_BY_DESC_INT){
			order = "desc";
		}
		return communicateEntityMapper.selectByUserId(userId,order,startIndex,count);
	}

	@Override
	public CommunicateEntity selectByContactId(String userId, String contactId) {		
		return communicateEntityMapper.selectByContactId(userId, contactId);
	}

	@Override
	public long countByUserId(String userId) {
		return communicateEntityMapper.countByUserId(userId);
	}
	
}
